import Vue from 'vue'
import Router from 'vue-router'

// 正常加载
// import Home from '../views/home'
// import About from '../views/About'


// 按需（懒）加载
function loadView (view, folder3, folder2, folder1) {
    if (folder1) {
        return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${folder1}/${folder2}/${folder3}/${view}.vue`)
    } else if (folder2) {
        return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${folder2}/${folder3}/${view}.vue`)
    } else if (folder3) {
        return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${folder3}/${view}.vue`)
    } else {
        return () => import(/* webpackChunkName: "view-[request]" */ `@/views/${view}.vue`)
    }
}


const routerView = `<transition name="fade-transform" mode="out-in"><router-view></router-view></transition>`;


Vue.use(Router)

let base = `${process.env.BASE_URL}` // 动态获取二级目录

const router = new Router({
    mode: 'history', // 路由有两种模式：history、hash，想要不带#号就选history，默认是hash模式
    base: base,
    routes: [
        {
            path: '/',
            name: 'dashboard',
            meta: { title: '首页', icon: 'el-icon-s-home' },
            component: loadView('index', 'dashboard')
        },
        {
            path: '/nested',
            name: 'nested',
            meta: { title: '嵌套路由', icon: 'el-icon-s-operation', breadcrumbClick: false },
            component: {
                render: (e) => e({
                    template: routerView
                })
            },
            children: [
                {
                    path: 'menu1',
                    name: 'menu1',
                    meta: { title: '菜单1' },
                    component: loadView('index', 'menu1', 'nested')
                },
                {
                    meta: { title: '菜单2', breadcrumbClick: false },
                    path: 'menu2',
                    name: 'menu2',
                    component: {
                        render: (e) => e({
                            template: routerView
                        })
                    },
                    children: [
                        {
                            path: 'menu2-1',
                            name: 'menu2-1',
                            meta: { title: '菜单2-1' },
                            component: loadView('index', 'menu2', 'nested')
                        },
                    ]
                },
            ]
        },
        {
            path: '/table',
            name: 'table',
            meta: { title: '表格示例', icon: 'el-icon-s-grid', breadcrumbClick: false },
            component: {
                render: (e) => e({
                    template: routerView
                })
            },
            children: [
                {
                    path: 'basicGrid',
                    name: 'basicGrid',
                    meta: { title: '基础表格' },
                    component: loadView('index', 'basicGrid', 'table'),
                    children: [
                        {
                            path: "basicGridDetails",
                            name: 'basicGridDetails',
                            meta: { title: '详情', activeMenu: 'basicGrid' },
                            hidden: true,
                            component: loadView('basicGridDetails', 'basicGrid', 'table'),
                        }
                    ]
                },
                {
                    path: 'grid',
                    name: 'grid',
                    meta: { title: 'grid' },
                    component: loadView('index', 'grid', 'table'),
                },
            ]
        },
        {
            path: '/form',
            name: 'form',
            meta: { title: 'form演示', icon: 'el-icon-s-order' },
            component: loadView('index', 'form')
        },
        {
            path: '/drag',
            name: 'drag',
            meta: { title: '拖拽示例', icon: 'el-icon-s-check' },
            component: loadView('index', 'drag')
        },
        {
            path: '/splitPane',
            name: 'splitPane',
            meta: { title: '宽度拖拽示例', icon: 'el-icon-star-on' },
            component: loadView('index', 'splitPane')
        },
        {
            path: '/tinymce',
            name: 'tinymce',
            meta: { title: '富文本演示', icon: 'el-icon-s-check' },
            component: loadView('index', 'tinymce')
        },
        {
            path: '/drawer',
            name: 'drawer',
            meta: { title: '抽屉演示', icon: 'el-icon-s-check' },
            component: loadView('index', 'drawer')
        },
        {
            path: '/tree',
            name: 'tree',
            meta: { title: 'tree演示', icon: 'el-icon-s-check' },
            component: loadView('index', 'tree')
        },
        {
            path: '/scottMap',
            name: 'scottMap',
            meta: { title: 'scottMap', icon: 'el-icon-s-check' },
            component: loadView('index', 'scottMap')
        },
        {
            path: '/systemManagement',
            name: 'systemManagement',
            meta: { title: '系统管理', icon: 'el-icon-s-operation', breadcrumbClick: false },
            component: {
                render: (e) => e({
                    template: routerView
                })
            },
            children: [
                {
                    path: 'user',
                    name: 'user',
                    meta: { title: '用户管理' },
                    component: loadView('index', 'user', 'systemManagement')
                },
                {
                    path: 'menu',
                    name: 'menu',
                    meta: { title: '菜单管理' },
                    component: loadView('index', 'menu', 'systemManagement')
                },
                {
                    path: 'role',
                    name: 'role',
                    meta: { title: '角色管理' },
                    component: loadView('index', 'role', 'systemManagement')
                },
            ]
        },
        {
            path: '/login',
            name: 'login',
            hidden: true,
            component: loadView('index', 'login')
        },

    ],
    scrollBehavior (to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
});

import store from "@/store"
// import filterRouter from "./permissions"
router.beforeEach((to, from, next) => {
    const isLogin = store.state.user.userInfo ? true : false
    if (!isLogin && to.name !== 'login') {
        router.push({ name: 'login' })
    } else {
        // if (router.options.routes.length === 2 && to.name != "login" && to.name !== 'unifiedLogin') {
        //     let asyncRoutes = filterRouter()
        //     router.addRoutes(asyncRoutes)
        //     router.options.routes = [...asyncRoutes, ...router.options.routes]
        //     return next({ ...to, replace: true })
        // }
    }
    next()
});

const routerPush = Router.prototype.push
Router.prototype.push = function push (location) {
    return routerPush.call(this, location).catch(error => error)
}


export default router;

